#!/usr/bin/env python

import math
import cairo,Image,glob
import random
import re

class Dessin3:
		
	def __init__(self,fichier):
		self.fichier=fichier
	
	def dessiner(self,fichier,listdico,step):
		regex=re.compile('(.*)\..*')
		result_regex=regex.findall(fichier)	
		fichierpng=result_regex[0]
		print fichierpng
		im =  Image.open(fichier)
		im.save(fichierpng+str("_final.png"),"PNG")
		surface = cairo.ImageSurface.create_from_png(fichierpng+str("_final.png"))
		ctx = cairo.Context (surface)
		ctx.scale(surface.get_width(),surface.get_height())
		WIDTH=surface.get_width()
		HEIGHT=surface.get_height()

		if WIDTH > HEIGHT :
			proportionX=(float)(HEIGHT)/(float)(WIDTH)
			proportionY=1.0
		else:
			proportionX=1.0
			proportionY=(float)(WIDTH)/(float)(HEIGHT)

		for elem in listdico:
				print elem
				largeur2=0.15+((float)(step)/(float)(WIDTH))
				largeur=0.015
				x=(float)(elem["x"])/(float)(WIDTH)
				y=(float)(elem["y"])/(float)(HEIGHT)
				# expression pour juste la valeur numerique
				
				regex=re.compile('([-0-9]*) .*')
				result_regex=regex.findall(elem["signal"])
				if result_regex:
					signal=int(result_regex[0])
					print signal
					ctx.save()
			
					if signal >  - 8.3:
						#marron fonce
						red=0.6
						green=0
						blue=0

					if signal >= -8.3 and signal < -16.6:	
						#marron clair
						red=0.6
						green=0.2
						blue=0

					if signal >= -25 and signal < - 16.6:

						#rouge fonce
						red=1
						green=0
						blue=0

					if signal >= -33.3 and signal <  -25  :
						#rouge clair
						red=1
						green=0.2
						blue=0

					if signal >=  -41.6 and signal < - 33.3 :
						#orange fonce
						red=1
						green=0.6
						blue=0

					if signal >=  -50 and signal < -41.6:
						#orange clair
						red=1
						green=0.8
						blue=0

					if signal >= -58.3 and signal <  -50:
						#jaune fonce
						red=0.9
						green=0.85
						blue=0

					if signal >= -66 and signal < -58.3   :
						#jaune clair
						red=1
						green=1
						blue=0

					if signal >= -75  and signal < - 66 :			
						#vert fonce
						red=0
						green=1
						blue=0

					if signal >= - 83.3 and signal<  -75:	
						#vert clair
						red=0
						green=1
						blue=0.6

					if signal >= - 91.6 and signal < - 83.3:
						#bleu fonce
						red=0.2
						green=0.4
						blue=1

					if signal < - 91.6:	

						#bleu clair
						print "la"
						red=0.4
						green=0.8
						blue=1

				#GESTION DE LA PROPORTION puis ecriture avec Cairo
					linear = cairo.LinearGradient(0, 0, 0, 1)
					linear.add_color_stop_rgb(blue, red, green,0.7)
					linear.add_color_stop_rgb(blue, red, green,0.1)

					if proportionX < 1:
						print "la "+str(proportionX)
						ctx.scale(proportionX,1)
						radial = cairo.RadialGradient(x*(1.0/proportionX), y, largeur, x*(1.0/proportionX), y,largeur2)
						radial.add_color_stop_rgba(0, 0, 0, 0, 1)
						radial.add_color_stop_rgba(0.5, 0, 0, 0, 0)

						ctx.set_source(linear)
						ctx.mask(radial)
						ctx.restore()
					if proportionY < 1 :
						print "la 2"+str(proportionY)
						ctx.scale(1,proportionY)
						radial = cairo.RadialGradient(x, y*(1.0/proportionY), largeur, x, y*(1.0/proportionY),largeur2)

						radial.add_color_stop_rgba(0, 0, 0, 0, 1)
						radial.add_color_stop_rgba(0.5, 0, 0, 0, 0)

						ctx.set_source(linear)
						ctx.mask(radial)
						ctx.restore()


		surface.write_to_png (fichierpng+str("_final.png")) # Output to PNG

		

if __name__=="__main__":
	obj=Dessin3("un")
	obj.dessiner("auditt.gif")
	



